﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Entity;
using BLL;


namespace BookStore.ShopCart
{
    public class CarFactory
    {
        const string carkey = "X_car";
        public static Cart GetCar()
        { 
            HttpSessionState session = HttpContext.Current.Session;
            if (session[carkey] == null)
            {
                Cart c = new Cart();
                session[carkey] = c;
            }
            return (Cart)session[carkey];
        }
       
    }

    public class Cart
    {
        //购物车表
        public int CartId { get; set; }
        public int UserID { get; set; }
        public int BookId { get; set; }
        public int CartCount { get; set; }
       
        List<CartItem> items = new List<CartItem>();
        public List<CartItem> Items
        {
            get { return items; }
            set { items = value; }
        }
        
        public void Add(BookInformatica book,int s,int dl)
        {
            CartItem item = items.SingleOrDefault(a => a.BookId == book.BookId);
            if (item != null)
            {
                item.Num += s;
            }
            else
            {
                item = new CartItem();
                item.Num = s;
                item.Price = (decimal)book.BookPrice;
                item.Img = book.BookPicture;
                item.BookName = book.BookName;
                item.BookId = book.BookId;
                items.Add(item);
            }
         
           //已经登陆的用户
            if (dl != 0)
            { 
         
                if (X_BShopcart.Carnum(dl)==0)
                {
                    X_BShopcart.Insertcart(dl);
                    //添加购物车
                }
                else
                {
                  int cid = X_BShopcart.Carnum(dl);
                  for (int i = 0; i < items.Count; i++)
                  {
                    List<CartItem> list = X_BShopcart.Getcaritem(items[i].BookId, cid);
                    if (list.Count == 0)
                    {
                        CartItem a = new CartItem();
                        a.BookId = 0;
                        list.Add(a);
                    }
                      if (list[0].BookId==items[i].BookId)
                      {
                          int num = list[0].Num + items[i].Num;
                          X_BShopcart.Updatashop(num, items[i].BookId, cid);
                      }
                      else
                      {
                          X_BShopcart.Insertcaritem(items[i], cid);
                      }
                  }
                }
            }


          
        }
        public void UpdataNum(int bookid, int n,int dl)
        {
            CartItem item = items.SingleOrDefault(a => a.BookId == bookid);
            if (item != null)
            {
                item.Num = n;
            }
              //已经登陆的用户
            if (dl != 0)
            {

                if (X_BShopcart.Carnum(dl) == 0)
                {
                    X_BShopcart.Insertcart(dl);
                    //添加购物车
                }
                else
                {
                    //购物车id
                    int cid = X_BShopcart.Carnum(dl);
                    X_BShopcart.Updatashop(n,bookid,cid);
                }
            }
        }
        public void Delcar(int bookid,int dl)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].BookId == bookid)
                {
                    items.Remove(items[i]);
                    return;
                }
            }
            //已经登陆的用户
            if (dl != 0)
            {

                if (X_BShopcart.Carnum(dl) != 0)
                {
                    //删除
                    int cid = X_BShopcart.Carnum(dl);
                    X_BShopcart.Delshop(bookid, cid);
                }

            }
        }
    }
    //public class CartItem
    //{
    //    public int Id { get; set; }
    //    public int CartId { get; set; }
    //    public int Num { get; set; }
    //    public decimal Price { get; set; }
    //    public string Img { get; set; }
    //    public string BookName { get; set; }
    //    public int BookId { get; set; }
    //}
}